package com.itheima.mapper;

import com.itheima.pojo.Item;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface ItemMapper {
    @Select("select * from tb_item")
    @ResultMap("itemResultMap")
    List<Item> selectAll();

    @Select("select * from tb_item where id = #{id}")
    @ResultMap("itemResultMap")
    Item selectById(Integer id);

    // 添加商品
    @Insert("insert into tb_item (item_name,seller_name,description,price,seller_id,sales_volume,inventory) values(#{itemName},#{sellerName},#{description},#{price},#{sellerId},0,#{inventory})") // 0是销量的默认值
    @ResultMap("itemResultMap")
    void add(Item item);

    // 根据卖家id选择
    @Select("select * from tb_item where seller_id = #{sellerId}")
    @ResultMap("itemResultMap")
    List<Item> selectBySellerId(Integer sellerId);

    // 修改商品信息
    @Update("update tb_item set item_name = #{itemName}, description = #{description}, price = #{price}, inventory = #{inventory} where id = #{id}")
    @ResultMap("itemResultMap")
    void update(@Param("id") Integer id, @Param("itemName") String itemName, @Param("description") String description, @Param("price") Integer price, @Param("inventory") Integer inventory);

    @Delete("delete from tb_item where id = #{id}")
    @ResultMap("itemResultMap")
    void deleteById(Integer id);

    // 更新商品销量
    @Update("update tb_item set sales_volume = #{salesVolume} where id = #{itemId}")
    @ResultMap("itemResultMap")
    void updateSalesVolume(@Param("salesVolume") Integer salesVolume, @Param("itemId") Integer itemId);
}
